package com.resifind.www.dao.impl;

import com.resifind.www.dao.BlacklistDao;
import com.resifind.www.dao.mapper.Mappers;
import com.resifind.www.po.Client;
import com.resifind.www.po.Merchant;
import com.resifind.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @author 36069
 */
public class BlacklistDaoImpl implements BlacklistDao {
    @Override
    public boolean queryBlacklistExist(int clientId, int merchantId, String blacklistType) {
        //language=MySQL
        String sql = "select count(*) from resi_find.blacklist where client_id = ? and merchant_id = ? and blacklist_type = ?";
        Object[] params = {clientId, merchantId, blacklistType};
        List<Integer> query = JdbcUtils.query(sql, params, rs -> rs.getInt(1));
        return query.getFirst() > 0;
    }

    @Override
    public boolean addBlacklist(int clientId, int merchantId, String blacklistType, Connection conn) {
        //language=MySQL
        String sql = "insert into resi_find.blacklist (client_id, merchant_id, blacklist_type) values (?, ?, ?)";
        Object[] params = {clientId, merchantId, blacklistType};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean deleteClientBlacklist(int clientId, List<Integer> merchantIds, Connection conn) {
        //language=MySQL
        String sql = "delete from resi_find.blacklist where client_id = ? and merchant_id = ? and blacklist_type = '客户拉黑'";
        for (Integer merchantId : merchantIds) {
            Object[] params = {clientId, merchantId};
            if (!JdbcUtils.update(sql, params, conn)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean deleteMerchantBlacklist(int merchantId, List<Integer> clientIds, Connection conn) {
        //language=MySQL
        String sql = "delete from resi_find.blacklist where client_id = ? and merchant_id = ? and blacklist_type = '商户拉黑'";
        for (Integer clientId : clientIds) {
            Object[] params = {clientId, merchantId};
            if (!JdbcUtils.update(sql, params, conn)) {
                return false;
            }
        }
        return true;
    }


    @Override
    public List<Merchant> queryBlackListByClientId(int clientId) {
        //language=MySQL
        String sql = """
                select m.*, 0 as is_favour from resi_find.merchant m
                join resi_find.blacklist b on m.id = b.merchant_id
                where b.client_id = ? and b.blacklist_type = '客户拉黑'
                """;
        Object[] params = {clientId};
        return Mappers.merchantMapper(sql, params);
    }

    @Override
    public List<Client> queryBlacklistByMerchantId(int merchantId) {
        //language=MySQL
        String sql = """
                select c.* from resi_find.blacklist b
                join resi_find.client c on b.client_id = c.id
                where b.merchant_id = ? and b.blacklist_type = '商户拉黑'
                """;
        Object[] params = {merchantId};
        return Mappers.clientMapper(sql, params);
    }
}
